﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Catsland.Core
{
	public class GameObjectList : UniqueList<GameObject>
	{

		List<GameObject> m_removeList;
		List<GameObject> m_addList;

		public override void AddItem(string guid, GameObject item)
		{
			if (m_addList == null)
			{
				m_addList = new List<GameObject>();
			}
            if (m_list != null && m_list.ContainsKey(item._guid))
            {
                Console.Out.WriteLine("Has contain guid: " + item._guid);
                return;
            }
			m_addList.Add(item);
		}

		public List<GameObject> GetAddList()
		{
			return m_addList;
		}

		public void UpdateAdd()
		{
			if (m_addList != null)
			{
				bool doSomething = false;
				foreach (GameObject gameObject in m_addList)
				{
					gameObject.Initialize(Mgr<Scene>.Singleton);
					base.AddItem(gameObject._guid, gameObject);
					doSomething = true;
				}

				if (doSomething && Mgr<GameEngine>.Singleton._gameEngineMode
							== GameEngine.GameEngineMode.MapEditor)
				{
					Mgr<GameEngine>.Singleton.Editor.UpdateGameObjectList(this);
				}

				m_addList.Clear();
			}
		}

		public override void ReleaseAll()
		{
			base.ReleaseAll();

			if (Mgr<GameEngine>.Singleton._gameEngineMode
				== GameEngine.GameEngineMode.MapEditor)
			{
				Mgr<GameEngine>.Singleton.Editor.UpdateGameObjectList(this);
			}
		}


		public override void RemoveItem(string guid)
		{
			if (m_removeList == null)
			{
				m_removeList = new List<GameObject>();
			}
			m_removeList.Add(m_list[guid]);

		}

		public void UpdateRemove()
		{
			if (m_removeList != null)
			{
				bool doSomething = false;
				foreach (GameObject gameObject in m_removeList)
				{
					m_list.Remove(gameObject._guid);
                    gameObject.Destroy();
					doSomething = true;
				}

				if (doSomething && Mgr<GameEngine>.Singleton._gameEngineMode
							== GameEngine.GameEngineMode.MapEditor)
				{
					Mgr<GameEngine>.Singleton.Editor.UpdateGameObjectList(this);
				}

				m_removeList.Clear();
			}
		}


		public void Update(int timeLastFrame)
		{
			if (m_list == null) 
			{
				return;
			}
			
			foreach (KeyValuePair<string, GameObject> keyValue in m_list)
			{
				keyValue.Value.Update(timeLastFrame);
			}

		}

        public void EditorUpdate(int timeLastFrame)
        {
            if (m_list == null)
            {
                return;
            }

            foreach (KeyValuePair<string, GameObject> keyValue in m_list)
            {
                keyValue.Value.EditorUpdate(timeLastFrame);
            }
        }

		public bool SaveToNode(XmlNode node, XmlDocument doc)
		{
			XmlElement gameobjects = doc.CreateElement("GameObjects");
			node.AppendChild(gameobjects);

			if (m_list != null)
			{
                foreach (KeyValuePair<string, GameObject> keyValue in m_list)
				{
					keyValue.Value.SaveToNode(gameobjects, doc);
				}
			}
			return true;
		}

		public static GameObjectList LoadFromNode(XmlNode node, Scene scene)
		{
			GameObjectList gameObjectList = new GameObjectList();
			foreach (XmlNode gameObject in node.ChildNodes)
			{
				GameObject newGameObject = GameObject.LoadFromNode(gameObject, scene);
				gameObjectList.AddItem(newGameObject._guid, newGameObject);
			}

			return gameObjectList;
		}
	}
}
